use super::{ground::Ground, Client, RNull, VBase, VMap, VRece};
use tracing::{instrument, warn};

#[instrument(skip_all, name = "queue_job")]
pub async fn queue_job(rx: &VRece, client: &Client) {
    loop {
        if let Err(e) = do_buffer(rx, client).await {
            warn!("queue error: {:?}", e);
        }
    }
}

#[instrument(skip_all, name = "buffer")]
async fn do_buffer(rx: &VRece, client: &Client) -> RNull {
    if let Ok(d) = rx.recv().await {
        warn!("buf len: {:?}", rx.len());

        let data = d.ckobj()?;

        let cmd = data.ck_str("cmd")?;

        let group = Ground;

        match cmd {
            "inbuf" => {
                // scan code
                group.do_inbuf(data, client).await?;
            }
            "idle" => {
                // teak out
                group.do_idle(data, client).await?;
            }
            _ => {
                warn!("buffer, no cmd");
            }
        }
    }

    Ok(())
}
